<template>
    <el-dialog
      custom-class="modify-password-dialog"
      title="Change Password"
      :visible.sync="visible"
      width="85%"
      @close="handleClose"
    >
      <el-form
        ref="modifyForm"
        :model="form"
        :rules="rules"
        label-width="70px"
        size="small"
      >
        <el-form-item label="Old" prop="oldPassword">
          <el-input
            v-model="form.oldPassword"
            type="password"
            show-password
            placeholder="current password"
          ></el-input>
        </el-form-item>
  
        <el-form-item label="New" prop="newPassword">
          <el-input
            v-model="form.newPassword"
            type="password"
            show-password
            placeholder="new password"
          ></el-input>
        </el-form-item>

        <el-form-item label="Confirm" prop="confirmPassword">
          <el-input
            v-model="form.confirmPassword"
            type="password"
            show-password
            placeholder="new password again"
          ></el-input>
        </el-form-item>
      </el-form>
  
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">Cancel</el-button>
        <el-button type="primary" @click="handleSubmit">Confirm</el-button>
      </span>
    </el-dialog>
</template>
  
<script>
  export default {
    name: 'ModifyPasswordDialog',
    props: {
      value: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        form: {
          oldPassword: '',
          newPassword: '',
          confirmPassword: ''
        },
        rules: {
          oldPassword: [
            { message: 'Please enter your current password', trigger: 'blur' },
          ],
          newPassword: [
            { message: 'Please enter new password', trigger: 'blur' },
            { min: 10, max:20, message: 'The password contains 10 to 20 characters', trigger: 'blur' },
            { 
              validator: (rule, value, callback) => {
                if (value === this.form.oldPassword) {
                  callback(new Error('The new password cannot be the same as the old password'));
                } else {
                  callback();
                }
              },
              trigger: 'blur'
            }
          ],
          confirmPassword: [
            { message: 'Please confirm the new password', trigger: 'blur' },
            {
              validator: (rule, value, callback) => {
                if (value !== this.form.newPassword) {
                  callback(new Error('The two passwords are inconsistent'));
                } else {
                  callback();
                }
              },
              trigger: ['blur', 'change']
            }
          ],
        }
      }
    },
    computed: {
      visible: {
        get() {
          return this.value
        },
        set(value) {
          this.$emit('input', value)
        }
      }
    },
    methods: {
      handleSubmit() {
        this.$refs.modifyForm.validate(valid => {
          if (!valid) return
          this.$emit('submit', {
            oldPassword: this.form.oldPassword,
            newPassword: this.form.newPassword
          })
          this.resetForm()
        })
      },
  
      handleClose() {
        this.resetForm()
        this.$emit('close')
      },
  
      resetForm() {
        this.$refs.modifyForm.resetFields()
      }
    }
  }
</script>

<style lang="scss">
.v-modal {
  background: transparent !important;
}
</style>

